home *** CD-ROM | disk | FTP | other *** search
- #include <Icons.h>
- #include <QDOffscreen.h>
- #include "QDUtils.h"
- #include "MenuUtils.h"
- #include "MenuSelect.h"
- #include "DefaultMDEF.h"
-
-
-
- static short itemCount;
-
- static void ItemRect(short item, Rect *r, Rect* menuRect);
- static void DrawTheMenu(MenuHandle theMenu, Rect *menuRect, Point hitPt, short* inItem);
- static void DoMenuChoice(MenuHandle theMenu, Rect *menuRect, Point hitPt, short* inItem);
- static short IMAX(short a, short b);
- static void DrawMenuItem(MenuHandle theMenu, Rect *menuRect, short which, Boolean hilite);
- static void DrawMenuItemText(MenuHandle theMenu, Rect r, short which, Boolean hilite);
-
-
-
- enum {kNoIconLeftMargin = 14};
-
- typedef struct
- {
- short font,size,lineHeight,leftMargin,rightMargin,cmdWidth,baseLine;
- }MenuFontData, *MenuFontPtr, **MenuFontDataHandle;
-
- static MenuFontData data;
-
-
- static void GetFontSettings()
- {
- data.font = 0;
- data.size = 0;
-
- data.lineHeight = 18;
- data.leftMargin = 40;
- data.rightMargin = 14;
- data.cmdWidth = 26;
- data.baseLine = 4;
- }
-
- static void ItemRect(short item, Rect *r, Rect* menuRect)
- {
- if ((item < 1) || (item > itemCount))
- SetRect(r, 0, 0, 0, 0);
- else
- SetRect(r, menuRect->left, menuRect->top + (data.lineHeight * (item-1)),
- menuRect->right, menuRect->top + (data.lineHeight * (item)));
- }
-
-
- static void DrawMenuItem(MenuHandle theMenu, Rect *menuRect, short which, Boolean hilite)
- {
- Rect r;
-
- //SetRGBBackColor(52224,39168,26112);
-
- ItemRect(which, &r,menuRect);
- if (hilite)
- {
- ForeColor(blackColor);
- PaintRect(&r);
- }
- else
- EraseMenuRect(&r);
-
- DrawMenuItemText(theMenu, r, which, hilite);
- }
-
-
-
- static void DrawMenuItemText(MenuHandle theMenu, Rect r, short which, Boolean hilite)
- {
- short cmdChar, markChar;
- Style itemStyle;
- RGBColor back = {0,0,0}, grayColor = {0x7777,0x7777,0x7777};
- Boolean itemDisabled = !GetItemEnabled(theMenu, which);
- GDHandle gd = GetCurrentGD( *(Point*) &r );
- Boolean useGrayPat = false;//(((CGrafPtr)qd.thePort)->portPixMap[0]->pixelSize == 1);
- Str255 itemStr;
- short iconID = 0;
- Point p;
- CGrafPtr savePort;
- GDHandle saveGD;
-
- GetGWorld(&savePort, &saveGD);
-
- back = savePort->rgbBkColor;
-
- TextFont(data.font);
- TextSize(data.size);
-
- GetItemIcon(theMenu, which, &iconID);
- if (iconID)
- {
- SetPt(&p, r.left + 24, (r.top + r.bottom) >> 1);
- PlotSmallMenuIcon(p, iconID + 256, itemDisabled ? ttDisabled : ttNone);
- }
-
- /*if (itemDisabled )
- TextMode(grayishTextOr);
- else
- TextMode(srcOr);*/
-
- GetItem(theMenu, which, itemStr);
-
-
- if (itemStr[1] == '-' )
- {
- /*if (useGrayPat)
- PenPat((ConstPatternParam)&qd.gray);
- else*/
- RGBForeColor(&grayColor);
-
- MoveTo(r.left, r.top + (data.lineHeight >>1));
- Line(r.right - r.left, 0);
- }
- else
- {
- if (hilite)
- RGBForeColor(&back);
- else
- {
- if (itemDisabled)
- RGBForeColor(&grayColor);
- else
- ForeColor(blackColor);
- }
-
- if (iconID)
- MoveTo(r.left + data.leftMargin, r.bottom - data.baseLine);
- else
- MoveTo(r.left + 16, r.bottom - data.baseLine);
- GetItemStyle(theMenu, which, &itemStyle);
- TextFace(itemStyle);
-
- DrawString(itemStr);
-
- TextFace(0);
- GetItemCmd(theMenu,which,&cmdChar);
- GetItemMark(theMenu, which, &markChar);
-
-
- // if it's a hier item, it has no mark or cmd to draw, just an arrow
-
- if (cmdChar == 0x1B)
- {
- Point aLoc;
- short y;
-
- if (itemDisabled )
- {
- /*if (useGrayPat)
- PenPat((ConstPatternParam)&qd.gray);
- else*/
- RGBForeColor(&grayColor);
- }
-
- SetPt(&aLoc, r.right - 7, r.top +2);
- for (y = 6 ; y >= 0 ; y -= 2 )
- {
- MoveTo(aLoc.h, aLoc.v);
- Line(0, y);
- aLoc.h = aLoc.h + 1;
- aLoc.v = aLoc.v + 1;
- }
- }
- else
- {
- if ( markChar ) // if there's a mark & it's not a hier item
- {
- TextFont(0); // switch to chicago for tick symbol
- MoveTo(r.left + 2, r.bottom - data.baseLine);
- DrawChar(markChar);
- TextFont(data.font); // back to geneva
- }
-
- // ordinary item. draw mark and or cmd character
-
- if (cmdChar ) // draw cmd char
- {
- char str[2];
- str[1] =0;
- MoveTo(r.right - data.cmdWidth, r.bottom - data.baseLine);
- TextFont(0); //switch to Chicago for clover leaf symbol
- str[0] = 0x11;
- DrawChar(0x11);
- TextFont(data.font); // switch back to geneva
- str[0] = cmdChar;
-
- DrawChar(cmdChar); // draw char itself
- }
-
- }
- }
-
- //PenPat(&qd.black);
- }
-
-
-
-
- static void DrawTheMenu(MenuHandle theMenu, Rect *menuRect, Point hitPt, short* inItem)
- {
- short x;
- Rect r;
-
- for (x = 1 ; x <= itemCount ; x++)
- {
- ItemRect(x, &r, menuRect);
- DrawMenuItemText(theMenu, r, x, false);
- }
-
- TextFont(0);
- TextSize(12);
- TextFace(0);
- }
-
-
- static void DoMenuChoice(MenuHandle theMenu, Rect *menuRect, Point hitPt, short* whichItem)
- {
- short inItem;
- short cmdChar;
-
- if (PtInRect(hitPt , menuRect) )
- inItem = ((hitPt.v - menuRect->top) / data.lineHeight) + 1;
- else
- inItem = 0;
-
- if (HMGetBalloons())
- {
- Rect r;
-
- ItemRect(inItem, &r, menuRect);
- ShowMenuBalloon(theMenu, *whichItem, inItem, &r);
- }
-
- // if it's in a disabled item then ignore it
- if (!GetItemEnabled(theMenu, inItem))
- inItem = 0;
-
- if (inItem == *whichItem)
- return;
-
- if (*whichItem) // unhilite last selected item
- {
- GetItemCmd(theMenu, *whichItem, &cmdChar);
- if (cmdChar == 0x1B) // last selected item was hierarchichal, so need to redraw
- DrawTheMenu(theMenu,menuRect,hitPt, whichItem);
- else // just unhilite last one
- {
- DrawMenuItem(theMenu, menuRect, *whichItem, false);
- }
- }
-
- if (inItem) // hilite the new selected item
- {
- DrawMenuItem(theMenu, menuRect, inItem, true);
- }
-
- TextFont(0);
- TextSize(12);
- TextFace(0);
-
- *whichItem = inItem;
- }
-
-
- static short IMAX(short a, short b)
- {
- return (a > b) ? a : b;
- }
-
- static void CalculateMenuSize(MenuHandle theMenu, Rect *menuRect, Point hitPt, short* inItem)
- {
- short h, v, longest, x, txtWidth;
- Str255 itemStr;
- short tempChar;
- Boolean hasIcon = false;
-
- TextFont(data.font);
- TextSize(data.size);
- longest = 0;
- for (x = 1 ; x <= itemCount ; x++)
- {
- short iconID = 0;
-
- GetItem(theMenu, x, itemStr);
- txtWidth = StringWidth(itemStr);
- GetItemCmd(theMenu, x, &tempChar);
- if (tempChar )
- txtWidth = txtWidth + data.cmdWidth;
- longest = IMAX(txtWidth, longest);
- if ((x <= 31) && (itemStr[1] == '-') ) // if item is a dash, make sure it is disabled
- theMenu[0]->enableFlags &= ~(1L << x);
-
- GetItemIcon(theMenu, x, &iconID);
- if (iconID)
- hasIcon = true;
- }
-
- if (hasIcon)
- h = longest + data.leftMargin + data.rightMargin;
- else
- h = longest + kNoIconLeftMargin + data.rightMargin;
- v = itemCount * data.lineHeight;
- theMenu[0]->menuWidth = h;
- theMenu[0]->menuHeight = v;
- TextFont(0);
- TextSize(12);
- }
-
-
- static void CalculatePopupMenuRect( MenuHandle theMenu, Rect *menuRect,
- Point hitPt, short* whichItem)
- {
- Rect r, screen;
- GDHandle gD;
- Point loc;
-
-
- loc.h = hitPt.v; // hitPt is upside down!! The h and the v are swapped.
- loc.v = hitPt.h;
- SetRect(&r, 0, 0, theMenu[0]->menuWidth, theMenu[0]->menuHeight);
- OffsetRect(&r, loc.h, loc.v - (data.lineHeight * (*whichItem -1)));
-
- if (! (gD = GetCurrentGD(loc)))
- gD = GetMainDevice();
-
- screen = gD[0]->gdRect;
- InsetRect(&screen, 4, 4);
- if (gD == GetMainDevice() )
- screen.top += 20;
-
- if (r.top < screen.top )
- OffsetRect(&r, 0, screen.top - r.top);
- if (r.bottom > screen.bottom )
- OffsetRect(&r, 0, screen.bottom - r.bottom);
- if (r.right > screen.right )
- OffsetRect(&r, screen.right - r.right, 0);
- if (r.left < screen.left )
- OffsetRect(&r, screen.left - r.left, 0);
-
- *menuRect = r;
- *whichItem = r.top;
- }
-
-
- pascal void DefaultMDEF( short msg, MenuHandle m, Rect *mRect,
- Point p, short* i)
- {
- itemCount = CountMItems(m);
- GetFontSettings();
-
- switch (msg)
- {
- case mDrawMsg:
- DrawTheMenu(m, mRect,p,i);
- break;
- case mChooseMsg:
- DoMenuChoice(m, mRect,p,i);
- break;
- case mSizeMsg:
- CalculateMenuSize(m, mRect,p,i);
- break;
- case mPopUpMsg:
- CalculatePopupMenuRect(m, mRect,p,i);
- break;
- }
-
- }